package com.ruoyi.standingbook.recycle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.standingbook.cleaning.domain.CleaningImportDetail;
import com.ruoyi.standingbook.recycle.domain.Recycle;
import com.ruoyi.standingbook.recycle.domain.vo.RecycleVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.standingbook.recycle.domain.bo.RecycleImportDetailBo;
import com.ruoyi.standingbook.recycle.domain.vo.RecycleImportDetailVo;
import com.ruoyi.standingbook.recycle.domain.RecycleImportDetail;
import com.ruoyi.standingbook.recycle.mapper.RecycleImportDetailMapper;
import com.ruoyi.standingbook.recycle.service.IRecycleImportDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 垃圾清运导入明细Service业务层处理
 *
 * @author cc
 * @date 2023-04-28
 */
@RequiredArgsConstructor
@Service
public class RecycleImportDetailServiceImpl implements IRecycleImportDetailService {

    private final RecycleImportDetailMapper baseMapper;

    private DictService dictService = SpringUtils.getBean(DictService.class);

    /**
     * 查询垃圾清运导入明细
     */
    @Override
    public RecycleImportDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询垃圾清运导入明细列表
     */
    @Override
    public TableDataInfo<RecycleImportDetailVo> queryPageList(RecycleImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RecycleImportDetail> lqw = buildQueryWrapper(bo);
        Page<RecycleImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        for(RecycleImportDetailVo rv:result.getRecords()){
            String garbageCategoryText=this.dictService.getDictLabel("erecord_recycle_garbage_category", rv.getGarbageCategory());
            rv.setGarbageCategoryText(garbageCategoryText);
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询垃圾清运导入明细列表
     */
    @Override
    public List<RecycleImportDetailVo> queryList(RecycleImportDetailBo bo) {
        LambdaQueryWrapper<RecycleImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RecycleImportDetail> buildQueryWrapper(RecycleImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RecycleImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), RecycleImportDetail::getBatchNo, bo.getBatchNo());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), RecycleImportDetail::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getGarbageCategory()), RecycleImportDetail::getGarbageCategory, bo.getGarbageCategory());
        lqw.eq(bo.getProcessStatus() != null, RecycleImportDetail::getProcessStatus, bo.getProcessStatus());
        lqw.between(params.get("beginDate") != null && params.get("endDate") != null,
            RecycleImportDetail::getDate ,params.get("beginDate"), params.get("endDate"));
        lqw.orderByDesc(RecycleImportDetail::getCreateTime);
        lqw.orderByDesc(RecycleImportDetail::getDate);
        return lqw;
    }

    /**
     * 新增垃圾清运导入明细
     */
    @Override
    public Boolean insertByBo(RecycleImportDetailBo bo) {
        RecycleImportDetail add = BeanUtil.toBean(bo, RecycleImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改垃圾清运导入明细
     */
    @Override
    public Boolean updateByBo(RecycleImportDetailBo bo) {
        RecycleImportDetail update = BeanUtil.toBean(bo, RecycleImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RecycleImportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除垃圾清运导入明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 批量插入
     */
    @Override
    public Boolean batchInsertVo(List<RecycleImportDetailVo> voList) {
        List<RecycleImportDetail> list= BeanCopyUtils.copyList(voList , RecycleImportDetail.class);
        return baseMapper.insertBatch(list);
    }
}
